package com.qyer.search.model;

import static com.qyer.commons.utils.CommonUtils.cast2Int;
import static com.qyer.commons.utils.CommonUtils.cast2String;

import org.apache.commons.collections4.MapUtils;

import java.util.Map;

/**
 * Created by tangzheng on 16/3/7.
 */
public class SetOutPlace {
  public static final String FIELD_NAME = "place";
  public static final String FIELD_ID = "id";
  private String place;
  private int id;

  public SetOutPlace(String place, int id) {
    this.place = place;
    this.id = id;
  }

  public SetOutPlace(String place) {
    this.place = place;
  }

  public SetOutPlace(int id) {
    this.id = id;
  }

  @Override public boolean equals(Object o) {
    if (this == o)
      return true;
    if (o == null || getClass() != o.getClass())
      return false;

    SetOutPlace that = (SetOutPlace) o;

    if (id != that.id)
      return false;
    return place.equals(that.place);

  }

  @Override public int hashCode() {
    int result = place.hashCode();
    result = 31 * result + id;
    return result;
  }

  @Override public String toString() {
    return "SetOutPlace{" +
      "place='" + place + '\'' +
      ", id=" + id +
      '}';
  }

  public int getId() {
    return id;
  }

  public void setId(int id) {
    this.id = id;
  }

  public String getPlace() {
    return place;
  }

  public void setPlace(String place) {
    this.place = place;
  }

  public static SetOutPlace buildPlace(Map<String, Object> dataMap) {
    if (MapUtils.isEmpty(dataMap)) {
      return null;
    }
    int id = cast2Int(dataMap.get(FIELD_ID));
    String place = cast2String(dataMap.get(FIELD_NAME));

    return new SetOutPlace(place, id);
  }
}
